package miniproj.models.groups;

import java.util.LinkedList;

import javax.xml.bind.annotation.*;

import miniproj.models.people.Teacher;

public class Teaching implements java.io.Serializable {

	private static final long serialVersionUID = -3961518066906779349L;

	private String name;

	private Teacher headTeacher;

	// Warning : the headTeacher can teach
	// but he is NOT in the teachers list !
	//
	@XmlElement
	private LinkedList<Teacher> teachers;

	@XmlElement
	private LinkedList<Group> groups;

	/**
	 * Instantiates a new teaching. (JavaBeans)
	 */
	public Teaching() {
		this.headTeacher = null;
		this.teachers = new LinkedList<Teacher>();
		this.groups = new LinkedList<Group>();
		this.name = "undefined";
	}

	/**
	 * Instantiates a new teaching.
	 * 
	 * @param headTeacher
	 *            the head teacher of the teaching
	 * @param name
	 *            the name of the teaching
	 */
	public Teaching(Teacher headTeacher, String name) {
		this.headTeacher = headTeacher;
		this.teachers = new LinkedList<Teacher>();
		this.groups = new LinkedList<Group>();
		this.name = name;
	}

	/**
	 * Instantiates a new teaching.
	 * 
	 * @param headTeacher
	 *            the head teacher of the teaching
	 * @param groups
	 *            the groups concerned by the teaching
	 * @param name
	 *            the name of the teaching
	 */
	public Teaching(Teacher headTeacher, LinkedList<Group> groups, String name) {
		this.headTeacher = headTeacher;
		this.teachers = new LinkedList<Teacher>();
		this.groups = groups;
		this.name = name;
	}

	/**
	 * Instantiates a new teaching.
	 * 
	 * @param headTeacher
	 *            the head teacher of the teaching
	 * @param teachers
	 *            the teachers who will teach this teaching.
	 * @param groups
	 *            the groups concerned by the teaching
	 * @param name
	 *            the name of the teaching
	 */
	public Teaching(Teacher headTeacher, LinkedList<Teacher> teachers,
			LinkedList<Group> groups, String name) {
		this.headTeacher = headTeacher;
		this.teachers = teachers;
		this.groups = groups;
		this.name = name;
	}

	/**
	 * Gets the head teacher of the teaching
	 * 
	 * @return the head teacher of the teaching
	 */
	public Teacher getHeadTeacher() {
		return this.headTeacher;
	}

	/**
	 * Gets the teachers of the teaching
	 * 
	 * @return the teachers of the teaching
	 */
	public LinkedList<Teacher> getTeachers() {
		return this.teachers;
	}

	/**
	 * Gets the students groups concerned by this teaching
	 * 
	 * @return the students groups of the teaching
	 */
	public LinkedList<Group> getStudentsGroups() {
		return this.groups;
	}

	/**
	 * Gets the name of the teaching
	 * 
	 * @return the name of the teaching
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name of the teaching
	 * 
	 * @param name
	 *            the new name of the teaching
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the head teacher of the teaching
	 * 
	 * @param headTeacher
	 *            the new head teacher of the teaching
	 */
	public void setHeadTeacher(Teacher headTeacher) {
		this.headTeacher = headTeacher;
	}

	@Override
	public String toString() {
		String toDisplay = "Teaching " + this.name + " ( ";

		if (this.groups.size() == 0)
			toDisplay += "no student group)";

		for (Group g : this.groups)
			toDisplay += g + " ";

		toDisplay += ")";

		return toDisplay + ")";
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Teaching))
			return false;

		return ((Teaching) o).name.equals(name);
	}

	/**
	 * Gets the teachings by head teacher name.
	 * 
	 * @param firstName
	 *            the first name of the teacher
	 * @param lastName
	 *            the last name of the teacher
	 * @param l
	 *            the list of the teachings
	 * @return the teachings by head teacher name
	 */
	public static LinkedList<Teaching> getTeachingsByHeadTeacherName(
			String firstName, String lastName, LinkedList<Teaching> l) {
		LinkedList<Teaching> toReturn = new LinkedList<Teaching>();

		for (Teaching t : l)
			if (t.headTeacher.equals(firstName, lastName))
				toReturn.add(t);

		return toReturn;
	}
}
